package com.xxx3biji;

import com.xxx2.Employee;
import org.junit.Test;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * stream的中间操作
 */

public class TestStreamAPI02 {

    List<Employee> emps = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.33),
            new Employee(103, "abc", 38, 3333.66),
            new Employee(104, "赵六", 10, 7777.11),
            new Employee(105, "bcd", 38, 5555.22),
            new Employee(105, "bcd", 38, 5555.22),
            new Employee(105, "bcd", 38, 5555.22),
            new Employee(104, "赵六", 10, 7777.11)
    );

    // 中间操作

    /**
     * 映射
     * map--接收Lambda,将元素转换成其他形式或提取信息。接收一个函数作为参数,(只影响这个元素)
     * 该函数会被应用到每个元素上,并将其映射成一个新的元素.
     * flatMap--接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
     */
    @Test
    public void test03() {
        List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd", "eee");
        list.stream()
                //.map(String::toUpperCase)
                .map(str -> str.toUpperCase())
                .forEach(System.out::println);

        System.out.println("-----------------------------");
        emps.stream()
                //Employee::getName
                .map(Employee::getSalary)
                .forEach(System.out::println);
        //--------------------------第一种------------------------
        //套
        Stream<Stream<Character>> taowa = list.stream()
                //      .map(str -> filterCharacter(str))
                .map(TestStreamAPI02::filterCharacter);//{{a,a,a},{b,b,b},{c,c,c}}
        taowa.forEach((sm) -> sm.forEach(System.out::println));

        //--------------------------第二种------------------------
        //不套
        Stream<Character> butaowa = list.stream()
                .flatMap(TestStreamAPI02::filterCharacter);//{a,a,a,b,b,b,c,c,c}
        butaowa.forEach(System.out::println);
        System.out.println("add()和addAll()");
        /**
         * 这类似于list.add()和addAll()
         */
        List<Object> list2 = new ArrayList<>();
        list2.add("111");
        list2.add("222");
        list2.add(list);
        System.out.println(list2);
        System.out.println("=========================");
        List<Object> list3 = new ArrayList<>();
        list3.add("111");
        list3.add("222");
        list3.addAll(list);
        System.out.println(list3);


    }

    public static Stream<Character> filterCharacter(String str) {
        List<Character> list = new ArrayList<>();
        for (char c : str.toCharArray()) {
            list.add(c);
        }
        return list.stream();
    }


    /**
     * 筛选与切片
     * filter--接收Lambda,从流中排除某些元素(断言)
     * limit--截断流,使其元素不超过给定数量
     * skip(n)--跳过元素,返回一个扔掉了前n个元素的流。若流中元素不足n个,则返回一个空流。与limit(n)互补
     * distinct--筛选,通过流所生成元素的hashCode()和equals()去除重复元素
     */
    //内部迭代:迭代操作由Stream API 完成
    @Test
    public void test01() {
        //中间操作:不会执行任何操作
        Stream<Employee> stream = emps.stream()
                .filter(employee -> {
                    System.out.println("Stream API 的中间操作");
                    return employee.getAge() > 35;
                });

        //终止操作:一次性执行全部内容,即“惰性求值”
        stream.forEach(System.out::println);
    }

    //外部迭代:自己迭代

    @Test
    public void test02() {
        //中间操作:不会执行任何操作
        Stream<Employee> stream = emps.stream()
                .filter(employee -> employee.getSalary() > 4000)
                .skip(2)
                .distinct();

        //终止操作:一次性执行全部内容,即“惰性求值”
        stream.forEach(System.out::println);
    }

    /**
     * 排序
     * sorted() : 自然排序
     * sorted(Comparator comparator) : 自定义排序
     */
    @Test
    public void test04() {
        List<String> list = Arrays.asList("eee", "bbb", "hhh", "ddd", "aaa");
        list.stream()
                .sorted()
                .forEach(System.out::println);

        System.out.println("===================================");

        emps.stream()
                .sorted((emp1, emp2) -> {
                    int num = emp1.getAge().compareTo(emp2.getAge());
                    num = num == 0 ? emp1.getSalary().compareTo(emp2.getSalary()) : num;
                    return num;
                })
                .forEach(System.out::println);
    }


    @Test
    public void test05() {
        List<Card> list = new ArrayList<>();
        list.add(new Card("1", LocalDate.now()));
//        list.add(new Card("2", LocalDate.of(2021,12,13)));
//        list.add(new Card("3", LocalDate.of(2021,12,12)));
//        list.add(new Card("4", LocalDate.of(2021,12,17)));
//        list.add(new Card("5", LocalDate.of(2021,12,16)));
        System.out.println(list);
        list.stream()
                .sorted((card1,card2)->
                    card2.getRiqi().compareTo(card1.getRiqi())
                )
                .forEach(System.out::println);

        System.out.println("===================================");

/*        emps.stream()
                .sorted((emp1, emp2) -> {
                    int num = emp1.getAge().compareTo(emp2.getAge());
                    num = num == 0 ? emp1.getSalary().compareTo(emp2.getSalary()) : num;
                    return num;
                })
                .forEach(System.out::println);*/
        String s = Long.valueOf(new Date().getTime()).toString();
        System.out.println(s);

    }
}
